package org.elucid.gadmin.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

import org.apache.log4j.Logger;
import org.elucid.gadmin.ServerInfo;
import org.elucid.gadmin.model.DatabaseItem;
import org.elucid.gadmin.ui.events.ExecuteActionListener;
import org.elucid.gadmin.ui.events.OpenActionListener;
import org.elucid.gadmin.ui.events.SaveActionListener;

/**
 * 
 * @author george
 * 
 */
public class GDialog extends JDialog   {

	private static final Logger log = Logger.getLogger(GFrame.class);
	
	private static final int DEFAULT_WIDTH = 800;
	private static final int DEFAULT_HEIGHT = 700;
	private static final int DEFAULT_X = 30;
	private static final int DEFAULT_Y = 30;

	boolean isDirty = true;

	public GTextPane textpane;
	public JToolBar toolbar;
	public JPanel resultsPane;
	public JComboBox dbCombo;

	public ServerInfo serverInfo;
	private File file = null;

	public JLabel statusLabel;
	public JProgressBar progress;

	public JLabel progressLabel;

	private JMenuBar menu;
	private JMenu jFileMenu;


	/**
	 * 
	 * @param databaseName
	 * @param queryIndex
	 * @param initialQuery
	 */
	public GDialog(final ServerInfo serverInfo, final String databaseName, int queryIndex, String initialQuery, List<DatabaseItem> dbList, String title) {
		log.debug("GDialog()");
		
		this.setServerInfo(serverInfo);
		this.setTitle(title);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLocation(queryIndex * DEFAULT_X, queryIndex * DEFAULT_Y);

		resultsPane = new JPanel();
		resultsPane.setLayout( new BorderLayout() );
		
		menu = new JMenuBar();
		jFileMenu = new JMenu("File");
		JMenuItem openMenuItem = new JMenuItem("Open...");
		jFileMenu.add(openMenuItem);
		openMenuItem.addActionListener( new OpenActionListener(this)  );
		JMenuItem saveMenuItem = new JMenuItem("Save");
		jFileMenu.add( saveMenuItem );
		saveMenuItem.addActionListener( new SaveActionListener(this) );
		JMenuItem saveAsMenuItem = new JMenuItem("Save As...");
		jFileMenu.add( saveAsMenuItem );
		saveAsMenuItem.addActionListener( new SaveActionListener(this, true) );
		
		menu.add( jFileMenu );
		
		this.setJMenuBar(menu);
		
		toolbar = new JToolBar();
		textpane = new GTextPane();
		
		textpane.registerKeyboardAction( new AbstractAction() {
			
			private static final long serialVersionUID = 4663362877670109580L;

			@Override
			public void actionPerformed(ActionEvent evt) {
			    	new ExecuteActionListener( GDialog.this ).actionPerformed(null);
			}
			
		}, KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), JComponent.WHEN_FOCUSED);

		dbCombo = new JComboBox( dbList.toArray() );
		if( databaseName==null || databaseName.equals("") )
			dbCombo.setSelectedIndex( dbList.size() - 1 );
		else
		{
			for( int i = 0 ; i < dbCombo.getItemCount(); i++ )
			{
				if( dbCombo.getItemAt(i).toString().equals( databaseName ) )
				{
					dbCombo.setSelectedIndex(i);
				}
			}
		}
		
		toolbar.add( dbCombo );
		this.getContentPane().add(toolbar, BorderLayout.NORTH);

		JButton jExecuteButton = new JButton("Execute");
		jExecuteButton.addActionListener(new ExecuteActionListener( GDialog.this ));
		toolbar.add(jExecuteButton);
		
		JPanel queryPane = new JPanel();
		queryPane.setMinimumSize(new Dimension(100, 100));
		queryPane.setPreferredSize(new Dimension(400, 400));
		queryPane.setLayout(new BorderLayout());
		
		JScrollPane textScrollpane = new JScrollPane( textpane);
		queryPane.add(textScrollpane, BorderLayout.CENTER);
		
		JPanel statusPanel = new JPanel();
		statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		this.add(statusPanel, BorderLayout.SOUTH);
		statusPanel.setPreferredSize(new Dimension(this.getWidth(), GFrame.STATUS_BAR_HEIGHT));
		statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
		statusLabel = new JLabel();
		statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
		statusPanel.add(statusLabel);
		
		
		progress = new JProgressBar();
		progress.setMaximumSize(new Dimension( GFrame.PROGRESS_WIDTH, GFrame.STATUS_BAR_HEIGHT ));
		statusPanel.add(progress);
		
		progressLabel = new JLabel("00:00:00");
		statusPanel.add(progressLabel);
		
		if (initialQuery != null) {
			textpane.setText(initialQuery);
		}

		resultsPane.setPreferredSize(new Dimension(200, 200));

		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, queryPane, resultsPane);
		split.setOpaque(true);

		this.getContentPane().add(split, BorderLayout.CENTER);
		this.setVisible(true);
		
		this.textpane.requestFocusInWindow();
	}

	public ServerInfo getServerInfo() {
		return serverInfo;
	}

	public void setServerInfo(ServerInfo serverInfo) {
		this.serverInfo = serverInfo;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	private static final long serialVersionUID = -2475174244762420616L;

	public JTextPane getTextPane() {
		return  textpane;
	}



	
}
